{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #T9 LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #LCCI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: getValidT9Words"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #T9键盘"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>在老式手机上，用户通过数字键盘输入，手机将提供与这些数字相匹配的单词列表。每个数字映射到0至4个字母。给定一个数字序列，实现一个算法来返回匹配单词的列表。你会得到一张含有有效单词的列表。映射如下图所示：</p>\n",
    "\n",
    "<p><img src=\"https://assets.leetcode-cn.com/aliyun-lc-upload/original_images/17_telephone_keypad.png\" style=\"width: 200px;\"></p>\n",
    "\n",
    "<p><strong>示例 1:</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> num = &quot;8733&quot;, words = [&quot;tree&quot;, &quot;used&quot;]\n",
    "<strong>输出:</strong> [&quot;tree&quot;, &quot;used&quot;]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre><strong>输入:</strong> num = &quot;2&quot;, words = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]\n",
    "<strong>输出:</strong> [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</pre>\n",
    "\n",
    "<p>提示：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>num.length &lt;= 1000</code></li>\n",
    "\t<li><code>words.length &lt;= 500</code></li>\n",
    "\t<li><code>words[i].length == num.length</code></li>\n",
    "\t<li><code>num</code>中不会出现 0, 1 这两个数字</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [t9-lcci](https://leetcode.cn/problems/t9-lcci/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [t9-lcci](https://leetcode.cn/problems/t9-lcci/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        n_dict={\"2\":\"abc\",\"3\":\"def\",\"4\":\"ghi\",\"5\":\"jkl\",\"6\":\"mno\",\"7\":\"pqrs\",\"8\":\"tuv\",\"9\":\"wxyz\"}\n",
    "        for k, v in enumerate(num):\n",
    "            words = [w for w in words if w[k] in n_dict[v]]\n",
    "        return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "  def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "    def mmmm(wstr:str, snum:str) -> bool:\n",
    "      keys = [\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"]\n",
    "      id = 0\n",
    "      for s in snum:\n",
    "        if wstr[id] in keys[int(s) -2]:\n",
    "          pass\n",
    "        else:\n",
    "          return False\n",
    "        id = id +1\n",
    "      return True\n",
    "\n",
    "    outlist = []\n",
    "    for word in words:\n",
    "      if mmmm(word, num):\n",
    "        outlist.append(word)\n",
    "    return outlist\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        tran_list=[[],[],['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l'],['m','n','o'],['p','q','r','s'],['t','u','v'],['w','x','y','z']]\n",
    "        res_ls=words\n",
    "        for i in range(len(num)):\n",
    "            res_ls=list(filter(lambda x:x[i] in tran_list[int(num[i])],res_ls))\n",
    "        return res_ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        phone=['_','!@#','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']\n",
    "        res=[]\n",
    "        n=len(num)\n",
    "        for word in words:\n",
    "            cnt=0\n",
    "            for i in range(n):\n",
    "                if word[i] in phone[int(num[i])]:\n",
    "                    cnt+=1\n",
    "            if cnt==n:\n",
    "                res.append(word)\n",
    "        return res\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "  def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "    def mmmm(wstr:str, snum:str) -> bool:\n",
    "      keys = [\"abc\",\"def\",\"ghi\",\"jkl\",\"mno\",\"pqrs\",\"tuv\",\"wxyz\"]\n",
    "      id = 0\n",
    "      for s in snum:\n",
    "        if wstr[id] in keys[int(s) -2]:\n",
    "          pass\n",
    "        else:\n",
    "          return False\n",
    "        id = id +1\n",
    "      return True\n",
    "\n",
    "    outlist = []\n",
    "    for word in words:\n",
    "      if mmmm(word, num):\n",
    "        outlist.append(word)\n",
    "    return outlist\n",
    "\n",
    "  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        mp = {'1':{}, '2':{'a','b','c'}, '3':{'d','e','f'}, '4':{'g','h','i'},\n",
    "              '5':{'j','k','l'}, '6':{'m','n','o'}, '7':{'p','q','r','s'},\n",
    "              '8':{'t','u','v'}, '9':{'w','x','y','z'}, '0':{}}\n",
    "\n",
    "        res = []\n",
    "        n = len(num)\n",
    "        for w in words:\n",
    "            if len(w) != n: continue\n",
    "            i = 0\n",
    "            while i < n:\n",
    "                if w[i] not in mp[num[i]]:\n",
    "                    break\n",
    "                i += 1\n",
    "            if i == n: res.append(w)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic = {'2':['a','b','c'],'3':['d','e','f'],'4':['g','h','i'],'5':['j','k','l'],'6':['m','n','o'],'7':['p','q','r','s'],'8':['t','u','v'],'9':['w','x','y','z']}\n",
    "\n",
    "        def check(word):\n",
    "            for i,c in enumerate(word):\n",
    "                if c not in dic[num[i]]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        return [word for word in words if check(word)]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "\n",
    "        res = [words[i] for i in range(len(words))]\n",
    "\n",
    "        hashmap = {\n",
    "            '2': ['a', 'b', 'c'],\n",
    "            '3': ['d', 'e', 'f'],\n",
    "            '4': ['g', 'h', 'i'],\n",
    "            '5': ['j', 'k', 'l'],\n",
    "            '6': ['m', 'n', 'o'],\n",
    "            '7': ['p', 'q', 'r', 's'],\n",
    "            '8': ['t', 'u', 'v'],\n",
    "            '9': ['w', 'x', 'y', 'z']\n",
    "        }\n",
    "\n",
    "        curn = 0\n",
    "        while curn < len(num):\n",
    "            curr = 0\n",
    "            while curr < len(res):\n",
    "                if res[curr][curn] not in hashmap[num[curn]]:\n",
    "                    res.remove(res[curr])\n",
    "                else:\n",
    "                    curr += 1\n",
    "                \n",
    "            curn += 1\n",
    "\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        data = {\n",
    "            \"a\": \"2\",\n",
    "            \"b\": \"2\",\n",
    "            \"c\": \"2\",\n",
    "            \"d\": \"3\",\n",
    "            \"e\": \"3\",\n",
    "            \"f\": \"3\",\n",
    "            \"g\": \"4\",\n",
    "            \"h\": \"4\",\n",
    "            \"i\": \"4\",\n",
    "            \"j\": \"5\",\n",
    "            \"k\": \"5\",\n",
    "            \"l\": \"5\",\n",
    "            \"m\": \"6\",\n",
    "            \"n\": \"6\",\n",
    "            \"o\": \"6\",\n",
    "            \"p\": \"7\",\n",
    "            \"q\": \"7\",\n",
    "            \"r\": \"7\",\n",
    "            \"s\": \"7\",\n",
    "            \"t\": \"8\",\n",
    "            \"u\": \"8\",\n",
    "            \"v\": \"8\",\n",
    "            \"w\": \"9\",\n",
    "            \"x\": \"9\",\n",
    "            \"y\": \"9\",\n",
    "            \"z\": \"9\"\n",
    "        }\n",
    "        res = []\n",
    "        for word in words:\n",
    "            tmp = \"\"\n",
    "            for c in word:\n",
    "                tmp += data[c]\n",
    "            if tmp == num:\n",
    "                res.append(word)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        s = []\n",
    "        l = ['abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']\n",
    "        for i in range(len(words)):\n",
    "            for j in range(len(words[i])):\n",
    "                if words[i][j] not in l[int(num[j])-2]:\n",
    "                    break\n",
    "                if words[i][j] in l[int(num[j])-2] and j == len(words[i])-1:\n",
    "                    s.append(words[i])\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        num_eng=[['a','b','c'],['d','e','f'],['g','h','i'],['j','k','l'],['m','n','o'],['p','q','r','s'],['t','u','v'],['w','x','y','z']]\n",
    "        words_new=[]\n",
    "        for word in words:\n",
    "            for i in range(len(word)):\n",
    "                if word[i] not in num_eng[int(num[i])-2]:\n",
    "                    break\n",
    "                elif (word[i] in num_eng[int(num[i])-2]) and i==len(word)-1:\n",
    "                    words_new.append(word)\n",
    "        return words_new\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        n_c_mapping = {2: \"abc\", 3: \"def\", 4:\"ghi\", 5:\"jkl\", 6:\"mno\", 7:\"pqrs\", 8:\"tuv\", 9: \"wxyz\"}\n",
    "        res = []\n",
    "        len_word = len(num)\n",
    "        for word in words:\n",
    "            is_ok = True\n",
    "            i = 0\n",
    "            while is_ok and i < len_word:\n",
    "                if word[i] not in n_c_mapping[int(num[i])]:\n",
    "                    is_ok = False\n",
    "                i += 1\n",
    "            if is_ok:\n",
    "                res.append(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic = {\n",
    "            '1': '',\n",
    "            '2': 'abc',\n",
    "            '3': 'def',\n",
    "            '4': 'ghi',\n",
    "            '5': 'jkl',\n",
    "            '6': 'mno',\n",
    "            '7': 'pqrs',\n",
    "            '8': 'tuv',\n",
    "            '9': 'wxyz',\n",
    "            '0': ''\n",
    "        }\n",
    "\n",
    "        \n",
    "        for i in range(len(num)):\n",
    "            words = [w for w in words if w[i] in dic[num[i]]]\n",
    "        \n",
    "        return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        nums = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']\n",
    "        char_num = {}\n",
    "        for i in range(len(nums)):\n",
    "            for c in nums[i]:\n",
    "                char_num[c] = str(i + 2)\n",
    "        \n",
    "        result = []\n",
    "        for word in words:\n",
    "            word_num = ''\n",
    "            for c in word:\n",
    "                word_num += char_num[c]\n",
    "            if word_num == num:\n",
    "                result.append(word)\n",
    "        return result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        strings = [\"abc\", \"def\", \"ghi\", \"jkl\", \"mno\", \"pqrs\", \"tuv\", \"wxyz\"]\n",
    "        def check(str,num):\n",
    "            for i,x in enumerate(num):\n",
    "                if str[i] not in strings[int(x)-2]:\n",
    "                    return False\n",
    "            return True\n",
    "        lis = []\n",
    "        for word in words:\n",
    "            if check(word, num):\n",
    "                lis.append(word)\n",
    "        return lis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        chars=[ chr(i+97) for i in range(0,26)]\n",
    "\n",
    "        dict_c={1:[]}\n",
    "\n",
    "        k=0\n",
    "        for i in range(2,7):\n",
    "            dict_c[i]=chars[k*3:k*3+3]\n",
    "            k+=1\n",
    "        dict_c[7]=['p','q','r','s']\n",
    "        dict_c[8]=['t','u','v']\n",
    "        dict_c[9]=['w','x','y','z']\n",
    "\n",
    "        valid=[]\n",
    "\n",
    "        for w in words:\n",
    "            flag=True\n",
    "            for c,n in zip(w,num):\n",
    "                if c not in dict_c[int(n)]:\n",
    "                    flag=False\n",
    "                    break\n",
    "            if flag:\n",
    "                valid.append(w)\n",
    "\n",
    "        return valid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        sdict = {'0': '_', '1': \"!@#\", '2': \"abc\", '3': \"def\", '4': \"ghi\", '5': \"jkl\", '6': \"mno\", '7': \"pqrs\", '8': \"tuv\", '9': \"wxyz\"}\n",
    "        res = []\n",
    "        n = len(num)\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for i in range(n):\n",
    "                if word[i] not in sdict[num[i]]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag: res.append(word)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        val = {'a':'2','b':'2','c':'2','d':'3','e':'3','f':'3','g':'4','h':'4','i':'4','j':'5','k':'5','l':'5','m':'6','n':'6','o':'6','p':'7','q':'7','r':'7','s':'7','t':'8','u':'8','v':'8','w':'9','x':'9','y':'9','z':'9'}\n",
    "        n = len(num)\n",
    "        res = []\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for i in range(n):\n",
    "                if val[word[i]]!=num[i]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                res.append(word)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        m = {\n",
    "            '2':{'a', 'b', 'c'},\n",
    "            '3':{'d', 'e', 'f'},\n",
    "            '4':{'g', 'h', 'i'},\n",
    "            '5':{'j', 'k', 'l'},\n",
    "            '6':{'m', 'n', 'o'},\n",
    "            '7':{'p', 'q', 'r', 's'},\n",
    "            '8':{'t', 'u', 'v'},\n",
    "            '9':{'w', 'x', 'y', 'z'}\n",
    "            }\n",
    "\n",
    "        def check(word):\n",
    "            for i, c in enumerate(word):\n",
    "                if c not in m[num[i]]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        return [word for word in words if check(word)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        has = [2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9]\n",
    "        n = len(num)\n",
    "        res = []\n",
    "        for i in words:\n",
    "            for inx,w in enumerate(i):\n",
    "                if has[ord(w)-97] != int(num[inx]): break\n",
    "            else:\n",
    "                res.append(i)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        charlist = ['','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']\n",
    "        ans = []\n",
    "        l = len(num)\n",
    "        for word in words:\n",
    "            i = 0\n",
    "            flag = 0\n",
    "            for i in range(l):\n",
    "                if not word[i] in charlist[int(num[i])]:\n",
    "                    flag = 1\n",
    "                    break\n",
    "            if flag == 0:\n",
    "                ans.append(word)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        nums = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']\n",
    "        char_num = {}\n",
    "        for i in range(len(nums)):\n",
    "            for c in nums[i]:\n",
    "                char_num[c] = str(i + 2)\n",
    "        \n",
    "        result = []\n",
    "        for word in words:\n",
    "            word_num = ''\n",
    "            for c in word:\n",
    "                word_num += char_num[c]\n",
    "            if word_num == num:\n",
    "                result.append(word)\n",
    "        return result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        nums = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqrs', 'tuv', 'wxyz']\n",
    "        char_num = {}\n",
    "        for i in range(len(nums)):\n",
    "            for c in nums[i]:\n",
    "                char_num[c] = str(i + 2)\n",
    "        \n",
    "        result = []\n",
    "        for word in words:\n",
    "            word_num = ''\n",
    "            for c in word:\n",
    "                word_num += char_num[c]\n",
    "            if word_num == num:\n",
    "                result.append(word)\n",
    "        return result\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        #哈希表快，难打\n",
    "        l1 = []\n",
    "        for i in range (len(words)):\n",
    "            x = \"\"\n",
    "            for k in range (len(words[i])):\n",
    "                target = ord(words[i][k]) - 96\n",
    "                if target <= 15:\n",
    "                    x += str((target - 1) // 3 + 2)\n",
    "                elif target >= 16 and target <= 19:\n",
    "                    x += \"7\"\n",
    "                elif target >= 20 and target <= 22:\n",
    "                    x += \"8\"\n",
    "                else:\n",
    "                    x += \"9\"\n",
    "            if x == num:\n",
    "                l1.append(words[i])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic = {'2':['a','b','c'],'3':['d','e','f'],'4':['g','h','i'],'5':['j','k','l'],'6':['m','n','o'],'7':['p','q','r','s'],'8':['t','u','v'],'9':['w','x','y','z']}\n",
    "\n",
    "        res = []\n",
    "\n",
    "        n = len(num)\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for i in range(n):\n",
    "                if word[i] not in dic[num[i]]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            \n",
    "            if flag:\n",
    "                res.append(word)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "keypad = {\n",
    "    2:[\"a\",\"b\",\"c\"],\n",
    "    3:[\"d\",\"e\",\"f\"],\n",
    "    4:[\"g\",\"h\",\"i\"],\n",
    "    5:[\"j\",\"k\",\"l\"],\n",
    "    6:[\"m\",\"n\",\"o\"],\n",
    "    7:[\"p\",\"q\",\"r\",\"s\"],\n",
    "    8:[\"t\",\"u\",\"v\"],\n",
    "    9:[\"w\",\"x\",\"y\",\"z\"]\n",
    "}\n",
    "\n",
    "def is_keypad(num,word) -> bool :\n",
    "    index = 0\n",
    "    for a in word:\n",
    "        if a in keypad[int(num[index])]:\n",
    "            index += 1\n",
    "        else:\n",
    "            return False\n",
    "        \n",
    "    return True\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        result = []\n",
    "        for word in words:\n",
    "            if is_keypad(num,word) :\n",
    "                result.append(word)\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        ans = words\n",
    "        xx = {'2': ['a','b','c'], '3':['d','e','f'], '4': ['g','h','i'],'5':['j','k','l'], '6':['m','n','o'], '7': ['p','q','r','s'],'8':['t','u','v'],'9':['w','y','z','x']}\n",
    "        for i, v in enumerate(num):\n",
    "            for word in words:\n",
    "                if word[i] not in xx[v]:\n",
    "                    ans.remove(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        hash={\"a\":\"2\",\"b\":\"2\",\"c\":\"2\",\"d\":\"3\",\"e\":\"3\",\"f\":\"3\",\"g\":\"4\",\"h\":\"4\",\"i\":\"4\",\"j\":\"5\",\"k\":\"5\",\"l\":\"5\",\"m\":\"6\",\"n\":\"6\",\"o\":\"6\",\"p\":\"7\",\"q\":\"7\",\"r\":\"7\",\"s\":\"7\",\"t\":\"8\",\"u\":\"8\",\"v\":\"8\",\"w\":\"9\",\"x\":\"9\",\"y\":\"9\",\"z\":\"9\"}\n",
    "        ans=[]\n",
    "        for item in words:\n",
    "            n=len(item)\n",
    "            ok=True\n",
    "            for i in range(n):\n",
    "                if hash[item[i]]!=num[i]:\n",
    "                    ok=False\n",
    "                    break\n",
    "            if ok==True:\n",
    "                ans.append(item)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        #哈希表快，难打\n",
    "        l1 = []\n",
    "        for i in range (len(words)):\n",
    "            x = \"\"\n",
    "            p = 0\n",
    "            for k in range (len(words[i])):\n",
    "                target = ord(words[i][k]) - 96\n",
    "                if target <= 15:\n",
    "                    x += str((target - 1) // 3 + 2)\n",
    "                elif target >= 16 and target <= 19:\n",
    "                    x += \"7\"\n",
    "                elif target >= 20 and target <= 22:\n",
    "                    x += \"8\"\n",
    "                else:\n",
    "                    x += \"9\"\n",
    "                if x[k] != num[k]:\n",
    "                    p += 1\n",
    "                    break\n",
    "            if p == 0:\n",
    "                l1.append(words[i])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        mp=['','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']\n",
    "        ans=[]\n",
    "        def dfs(word, num):\n",
    "            if len(word)!=len(num):\n",
    "                return False\n",
    "            for w, n in zip(word,num):\n",
    "                if w not in mp[int(n)]:\n",
    "                    return False\n",
    "            return True\n",
    "        for word in words:\n",
    "            if dfs(word,num):\n",
    "                ans.append(word)\n",
    "        return ans\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        l1 = []\n",
    "        # l3 = []\n",
    "        # l2 = {\n",
    "        #     \"2\" : {\"a\",\"b\",\"c\"},\n",
    "        #     \"3\" : {\"d\",\"e\",\"f\"},\n",
    "        #     \"4\" : {\"g\",\"h\",\"i\"},\n",
    "        #     \"5\" : {\"j\",\"k\",\"l\"},\n",
    "        #     \"6\" : {\"m\",\"n\",\"o\"},\n",
    "        #     \"7\" : {\"p\",\"q\",\"r\",\"s\"},\n",
    "        #     \"8\" : {\"t\",\"u\",\"v\"},\n",
    "        #     \"9\" : {\"w\",\"x\",\"y\",\"z\"}\n",
    "        # }\n",
    "        # for i in range (len(words)):\n",
    "        #     if len(words) == \n",
    "\n",
    "        for i in range (len(words)):\n",
    "            x = \"\"\n",
    "            for k in range (len(words[i])):\n",
    "                target = ord(words[i][k]) - 96\n",
    "                if target <= 15:\n",
    "                    x += str((target - 1) // 3 + 2)\n",
    "                elif target >= 16 and target <= 19:\n",
    "                    x += \"7\"\n",
    "                elif target >= 20 and target <= 22:\n",
    "                    x += \"8\"\n",
    "                else:\n",
    "                    x += \"9\"\n",
    "            if x == num:\n",
    "                l1.append(words[i])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        d = {\n",
    "            '2':'abc',\n",
    "            '3':'def',\n",
    "            '4':'ghi',\n",
    "            '5':'jkl',\n",
    "            '6':'mno',\n",
    "            '7':'pqrs',\n",
    "            '8':'tuv',\n",
    "            '9':'wxyz'\n",
    "        }\n",
    "    \n",
    "        ret = []\n",
    "        for word in words:\n",
    "            b = 1\n",
    "            for i in range(len(num)):\n",
    "                if word[i] not in d[num[i]]:\n",
    "                    b = 0\n",
    "                    break\n",
    "            if b == 1:\n",
    "                ret.append(word)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        val = {'a':'2','b':'2','c':'2','d':'3','e':'3','f':'3','g':'4','h':'4','i':'4','j':'5','k':'5','l':'5','m':'6','n':'6','o':'6','p':'7','q':'7','r':'7','s':'7','t':'8','u':'8','v':'8','w':'9','x':'9','y':'9','z':'9'}\n",
    "        n = len(num)\n",
    "        res = []\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for i in range(n):\n",
    "                if val[word[i]]!=num[i]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                res.append(word)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        d = {\n",
    "            '2':'abc',\n",
    "            '3':'def',\n",
    "            '4':'ghi',\n",
    "            '5':'jkl',\n",
    "            '6':'mno',\n",
    "            '7':'pqrs',\n",
    "            '8':'tuv',\n",
    "            '9':'wxyz'\n",
    "        }\n",
    "     \n",
    "        n = len(num)\n",
    "        nn = len(words)\n",
    "        l = [1] * nn\n",
    "        for i in range(n):\n",
    "            for j in range(nn):\n",
    "                if l[j] == 1 and words[j][i] not in d[num[i]]:\n",
    "                    l[j] = 0\n",
    "    \n",
    "        ret = []\n",
    "        for i in range(nn):\n",
    "            if l[i] == 1:\n",
    "                ret.append(words[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic = {\n",
    "            '1': '',\n",
    "            '2': 'abc',\n",
    "            '3': 'def',\n",
    "            '4': 'ghi',\n",
    "            '5': 'jkl',\n",
    "            '6': 'mno',\n",
    "            '7': 'pqrs',\n",
    "            '8': 'tuv',\n",
    "            '9': 'wxyz',\n",
    "            '0': ''\n",
    "        }\n",
    "\n",
    "        \n",
    "        for i in range(len(num)):\n",
    "            words = [w for w in words if w[i] in dic[num[i]]]\n",
    "        \n",
    "        return words"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        kb = {'2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}\n",
    "        ans = []\n",
    "\n",
    "        for word in words:\n",
    "            for n, char in zip(num, word):\n",
    "                if char not in kb[n]:\n",
    "                    break\n",
    "            else:\n",
    "                ans.append(word)\n",
    "\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        #哈希表快，难打\n",
    "        l1 = []\n",
    "        for i in range (len(words)):\n",
    "            x = \"\"\n",
    "            p = 0\n",
    "            for k in range (len(words[i])):\n",
    "                target = ord(words[i][k]) - 96\n",
    "                if target <= 15:\n",
    "                    x += str((target - 1) // 3 + 2)\n",
    "                elif target >= 16 and target <= 19:\n",
    "                    x += \"7\"\n",
    "                elif target >= 20 and target <= 22:\n",
    "                    x += \"8\"\n",
    "                else:\n",
    "                    x += \"9\"\n",
    "                if x[k] != num[k]:\n",
    "                    p += 1\n",
    "                    break\n",
    "            if p == 0:\n",
    "                l1.append(words[i])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        d={}\n",
    "        k=0\n",
    "        for i in range(2,10):\n",
    "            for j in range(3):\n",
    "                d[chr(ord('a')+k)]=i\n",
    "                k+=1\n",
    "            if i in [7,9]:\n",
    "                d[chr(ord('a')+k)]=i\n",
    "                k+=1\n",
    "        res=[]\n",
    "        for word in words:\n",
    "            n=\"\".join([str(d[c])for c in word])\n",
    "            if n==num:\n",
    "                res.append(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "ARR = [\n",
    "    -1,  \n",
    "    2, 2, 2, 3, 3, 3, \n",
    "    4, 4, 4, 5, 5, 5, 6, 6, 6, \n",
    "    7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 9\n",
    "]\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        num = list(map(int, num))\n",
    "\n",
    "        return list(filter(\n",
    "            lambda word: all(ARR[ord(c) & 31] == d for c, d in zip(word, num)), \n",
    "            words\n",
    "        ))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        dic = {'2': \"abc\", '3': \"def\", '4': \"ghi\", '5': \"jkl\", '6': \"mno\", '7': \"pqrs\", '8': \"tuv\", '9': \"wxyz\"}\n",
    "        for i in words:\n",
    "            len_i = len(i)\n",
    "            word = False\n",
    "            for j in range(len_i):\n",
    "                if i[j] in dic[num[j]]:\n",
    "                    word = True\n",
    "                else:\n",
    "                    word = False\n",
    "                    break\n",
    "            if word:\n",
    "                res.append(i)\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        m = {\n",
    "            '2':{'a', 'b', 'c'},\n",
    "            '3':{'d', 'e', 'f'},\n",
    "            '4':{'g', 'h', 'i'},\n",
    "            '5':{'j', 'k', 'l'},\n",
    "            '6':{'m', 'n', 'o'},\n",
    "            '7':{'p', 'q', 'r', 's'},\n",
    "            '8':{'t', 'u', 'v'},\n",
    "            '9':{'w', 'x', 'y', 'z'}\n",
    "            }\n",
    " \n",
    "        def check(word):\n",
    "            for i, c in enumerate(word):\n",
    "                if c not in m[num[i]]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        return [word for word in words if check(word)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic = {}\n",
    "        dic['2'] = (\"a\",\"b\",\"c\")\n",
    "        dic['3'] = (\"d\",\"e\",\"f\")\n",
    "        dic['4'] = (\"g\",\"h\",\"i\")\n",
    "        dic['5'] = (\"j\",\"k\",\"l\")\n",
    "        dic['6'] = (\"m\",\"n\",\"o\")\n",
    "        dic['7'] = (\"p\",\"q\",\"r\",\"s\")\n",
    "        dic['8'] = (\"t\",\"u\",\"v\")\n",
    "        dic['9'] = (\"w\",\"x\",\"y\",\"z\")\n",
    "        res = []\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for i in range(len(word)):\n",
    "                if word[i] not in dic[num[i]]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                res.append(word)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dir = {\"2\":\"abc\",\"3\":\"def\",\"4\":\"ghi\",\"5\":\"jkl\",\"6\":\"mno\",\"7\":\"pqrs\",\"8\":\"tuv\",\"9\":\"wxyz\"}\n",
    "        ans = []\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for i in range(len(word)):\n",
    "                if word[i] not in dir[num[i]]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                ans.append(word)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        hash={\"a\":2,\"b\":2,\"c\":2,\"d\":3,\"e\":3,\"f\":3,\"g\":4,\"h\":4,\"i\":4,\"j\":5,\"k\":5,\"l\":5,\"m\":6,\"n\":6,\"o\":6,\"p\":7,\"q\":7,\"r\":7,\"s\":7,\"t\":8,\"u\":8,\"v\":8,\"w\":9,\"x\":9,\"y\":9,\"z\":9}\n",
    "        ans=[]\n",
    "        for item in words:\n",
    "            n=len(item)\n",
    "            ok=True\n",
    "            for i in range(n):\n",
    "                if hash[item[i]]!=int(num[i]):\n",
    "                    ok=False\n",
    "                    break\n",
    "            if ok==True:\n",
    "                ans.append(item)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        d = {\n",
    "            '2':'abc',\n",
    "            '3':'def',\n",
    "            '4':'ghi',\n",
    "            '5':'jkl',\n",
    "            '6':'mno',\n",
    "            '7':'pqrs',\n",
    "            '8':'tuv',\n",
    "            '9':'wxyz'\n",
    "        }\n",
    "        \n",
    "        dw = dict()\n",
    "        for w in words:\n",
    "            dw[w] = 1\n",
    "\n",
    "        n = len(num)\n",
    "        for i in range(n):\n",
    "            for w in dw:\n",
    "                if dw[w] == 1 and w[i] not in d[num[i]]:\n",
    "                    dw[w] = 0\n",
    "    \n",
    "        ret = []\n",
    "        for w in dw:\n",
    "            if dw[w] == 1:\n",
    "                ret.append(w)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        l1 = []\n",
    "        for i in range (len(words)):\n",
    "            if len(words[i]) == len(num):\n",
    "                x = \"\"\n",
    "                for k in range (len(words[i])):\n",
    "                    target = ord(words[i][k]) - 96\n",
    "                    if target <= 15:\n",
    "                        x += str((target - 1) // 3 + 2)\n",
    "                    elif target >= 16 and target <= 19:\n",
    "                        x += \"7\"\n",
    "                    elif target >= 20 and target <= 22:\n",
    "                        x += \"8\"\n",
    "                    else:\n",
    "                        x += \"9\"\n",
    "                if x == num:\n",
    "                    l1.append(words[i])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic = {\n",
    "            '2':{'a', 'b', 'c'},\n",
    "            '3':{'d', 'e', 'f'},\n",
    "            '4':{'g', 'h', 'i'},\n",
    "            '5':{'j', 'k', 'l'},\n",
    "            '6':{'m', 'n', 'o'},\n",
    "            '7':{'p', 'q', 'r', 's'},\n",
    "            '8':{'t', 'u', 'v'},\n",
    "            '9':{'w', 'x', 'y', 'z'}\n",
    "            }\n",
    "        \n",
    "        def recur(word):\n",
    "            for i, ch in enumerate(word):\n",
    "                if ch not in dic[num[i]]:\n",
    "                    return False\n",
    "            return True\n",
    "\n",
    "        res = []\n",
    "        for i in range(len(words)):\n",
    "            if recur(words[i]):\n",
    "                res.append(words[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        l1 = []\n",
    "        for i in range (len(words)):\n",
    "            if len(words[i]) == len(num):\n",
    "                x = \"\"\n",
    "                for k in range (len(words[i])):\n",
    "                    target = ord(words[i][k]) - 96\n",
    "                    if target <= 15:\n",
    "                        x += str((target - 1) // 3 + 2)\n",
    "                    elif target >= 16 and target <= 19:\n",
    "                        x += \"7\"\n",
    "                    elif target >= 20 and target <= 22:\n",
    "                        x += \"8\"\n",
    "                    else:\n",
    "                        x += \"9\"\n",
    "                if x == num:\n",
    "                    l1.append(words[i])\n",
    "        return l1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        hasmap = {\"2\": \"abc\", \"3\": \"def\", \"4\": \"ghi\", \"5\": \"jkl\", \"6\": \"mno\", \"7\": \"pqrs\", \"8\": \"tuv\", \"9\": \"wxyz\"}\n",
    "        ans = []\n",
    "        n = len(num)\n",
    "        for w in words:\n",
    "            flag = True\n",
    "            for i in range(n):\n",
    "                if w[i] not in hasmap[num[i]]:\n",
    "                    flag = False\n",
    "                    break\n",
    "            if flag:\n",
    "                ans.append(w)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        d = {}\n",
    "        d[2]=list(\"abc\")\n",
    "        d[3]=list(\"def\")\n",
    "        d[4]=list(\"ghi\")\n",
    "        d[5]=list(\"jkl\")\n",
    "        d[6]=list(\"mno\")\n",
    "        d[7]=list(\"pqrs\")\n",
    "        d[8]=list(\"tuv\")\n",
    "        d[9]=list(\"wxyz\")\n",
    "        d2 = {}\n",
    "        ret = []\n",
    "        for i in d.keys():\n",
    "            for j in d[i]:\n",
    "                d2[j]=str(i)\n",
    "        # print(d2)\n",
    "        for i in words:\n",
    "            if len(i)!= len(num):\n",
    "                continue\n",
    "            else:\n",
    "                ret.append(i)\n",
    "                # print(ret)\n",
    "                for j,c in enumerate(i):      \n",
    "                    if num[j] != d2[c]:\n",
    "                        # print(num[j],c)\n",
    "                        ret = ret[:-1]\n",
    "                        break\n",
    "        return ret\n",
    "        # ret = []\n",
    "        # def dfs(depth):\n",
    "        #     i= int(words[depth])\n",
    "        #     for c in d[i]:\n",
    "        #         for "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        dic={\"a\":2,\"b\":2,\"c\":2,\"d\":3,\"e\":3,\"f\":3,\"g\":4,\"h\":4,\"i\":4,\n",
    "            \"j\":5,\"k\":5,\"l\":5,\"m\":6,\"n\":6,\"o\":6,\"p\":7,\"q\":7,\"r\":7,\"s\":7,\n",
    "            \"t\":8,\"u\":8,\"v\":8,\"w\":9,\"x\":9,\"y\":9,\"z\":9}\n",
    "        ans=[]\n",
    "        for word in words:\n",
    "            if len(word)!=len(num):continue\n",
    "            flag=1\n",
    "            for i in range(len(word)):\n",
    "                if dic[word[i]]!=int(num[i]):\n",
    "                    flag=0\n",
    "                    continue\n",
    "            if flag:\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        d = {'a' : 2, 'b' : 2, 'c' : 2, 'd' : 3, 'e' : 3, 'f' : 3, 'g' : 4, 'h' : 4, 'i' : 4, 'j' : 5, 'k' : 5, 'l' : 5, 'm' : 6, 'n' : 6, 'o' : 6, 'p' : 7, 'q' : 7, 'r' : 7, 's' : 7, 't' : 8, 'u' : 8, 'v' : 8, 'w' : 9, 'x' : 9, 'y' : 9, 'z' : 9}\n",
    "        def match(nums, word):\n",
    "            for i in range(len(nums)):\n",
    "                if d[word[i]] != nums[i]:\n",
    "                    return False\n",
    "            return True\n",
    "        num = list(num)\n",
    "        num = [int(x) for x in num]\n",
    "        ans = []\n",
    "        for word in words:\n",
    "            if match(num, word):\n",
    "                ans.append(word)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import *\n",
    "from sortedcontainers import *\n",
    "from heapq import *\n",
    "from sys import *\n",
    "from math import *\n",
    "from itertools import *\n",
    "from functools import *\n",
    "from string import *\n",
    "from bisect import *\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t9 = {2:'abc', 3:'def', 4:'ghi', 5:'jkl', 6:'mno', 7:'pqrs', 8:'tuv', 9:'wxyz'}\n",
    "        for word in words:\n",
    "            flag = True\n",
    "            for x, y in zip(num, word):\n",
    "                if y not in t9[int(x)]: \n",
    "                    flag = False\n",
    "                    break \n",
    "            if flag:\n",
    "                ans.append(word)\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def getValidT9Words(self, num: str, words: List[str]) -> List[str]:\n",
    "        alpha_strs = 'abcdefghijklmnopqrstuvwxyz'\n",
    "        num_strs = '22233344455566677778889999'\n",
    "        alpha_to_num = {}\n",
    "        for alpha_char, num_char in zip(alpha_strs, num_strs):\n",
    "            alpha_to_num[alpha_char] = num_char\n",
    "        # print(\"alpha_to_num\", alpha_to_num)\n",
    "\n",
    "        words_trans_list = []\n",
    "        for word in words:\n",
    "            words_trans_list.append(''.join(map(lambda alpha_char: alpha_to_num[alpha_char], word)))\n",
    "        \n",
    "        # print(\"words_trans_list\", words_trans_list)\n",
    "\n",
    "        res_words = []\n",
    "        for index, word_trans in enumerate(words_trans_list):\n",
    "            if word_trans == num:\n",
    "                res_words.append(words[index])\n",
    "        \n",
    "        return res_words"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
